package org.geektimes.rest;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

/**
 * @author Lerke
 */
public class RestClientDemo {

    public static void main(String[] args) {
        testPost();
    }

    public static void testPost(){
        Client client = ClientBuilder.newClient();
        //设置RequestBody,如JSON字符串 {"username":"***","password":"***"}
        Entity<String> entity = Entity.json("***");
        Response response = client
                //设置请求URL 如:https://www.baidu.com
                .target("***")
                // Invocation.Builder
                .request()
                // Response
                .post(entity);
        //获取请求接口
        String content = response.readEntity(String.class);
        System.out.println(content);
    }

    public static void testGet(){
        Client client = ClientBuilder.newBuilder().build();
        Response response = client
                // WebTarget
                .target("https://www.baidu.com/")
                // Invocation.Builder
                .request()
                // Response
                .get();
        String content = response.readEntity(String.class);
        System.out.println(content);
    }
}
